package com.mazaiting.auth.security.core.userdetails.member;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.mazaiting.auth.security.core.userdetails.base.BaseUserDetailsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * 用户认证服务
 */
@Slf4j
@Service("memberUserDetailsService")
@RequiredArgsConstructor
public class MemberUserDetailsServiceImpl extends BaseUserDetailsService {

    private final WxMaService wxMaService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        return null;
    }

    /**
     * openid 认证方式
     *
     * @param openId 微信 OPEN_ID
     * @return 用户详细信息
     */
    public MemberUserDetails loadUserByOpenId(String openId) {
        log.info("微信 OPEN_ID: {} ", openId);
        // 创建用户详情
        return getUserDetails();
    }

    /**
     * 获取用户详情
     */
    private MemberUserDetails getUserDetails() {
        return new Random().nextInt() % 2 == 0 ? new MemberUserDetails() : null;
    }

}
